@echo off
setlocal enabledelayedexpansion

if "%1"=="install" goto install
if "%1"=="deinstall" goto deinstall
if "%1"=="start" goto start
if "%1"=="stop" goto stop

:usage
set "msg=%0 install|deinstall|start|stop"
echo Usage: !msg!
exit 0

:install
set inputfile=js-cron.xml.tmpl
set WSCRIPT_COMMAND=WSCRIPT_COMMAND
set WSCRIPT_COMMAND_VALUE=%SystemRoot%\System32\wscript.exe
set JS_CRON_DIR=JS_CRON_DIR
set JS_CRON_DIR_VALUE=%cd%
echo xml >js-cron.xml
del /F js-cron.xml
for /F "tokens=* delims=," %%n in (!inputfile!) do ( 
	set line=%%n
	set line=!line:%WSCRIPT_COMMAND%=%WSCRIPT_COMMAND_VALUE%!
	set line=!line:%JS_CRON_DIR%=%JS_CRON_DIR_VALUE%!
	echo !line!>>js-cron.xml
)
schtasks.exe /Create /TN js-cron-task /XML js-cron.xml
del /F js-cron.xml
exit 0

:deinstall
schtasks.exe /Delete /F /TN js-cron-task
exit 0

:start
schtasks.exe /Run /TN js-cron-task
exit 0

:stop
schtasks.exe /End /TN js-cron-task
exit 0
